class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
        unordered_map<int, vector<int>> edges;
        vector<int> in(numCourses, 0), ret;
        for(auto& e : prerequisites) // 建表
        {
            int a = e[0], b = e[1]; // b指向a
            edges[b].push_back(a);
            in[a]++;
        }
 
        queue<int> q;
        for(int i = 0; i < numCourses; ++i) // 处理入度为0的点
        {
            if(in[i] == 0)
                q.push(i);
        }
        while(!q.empty()) // BFS
        {
            int tmp = q.front(); // 度为0的点指向的点的度减一
            q.pop();
            ret.push_back(tmp);
            for(auto& e : edges[tmp])
            {
                in[e]--;
                if(in[e] == 0)
                    q.push(e);
            }
        }
        if(ret.size() == numCourses) // 无环大小就相等
            return ret;
        else
            return {};
    }
};